package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.ChangJingMianBan0Faa8A02CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Title:场景面板 <／p>
 * <p>Description: <／p>
 * <p>Company: JiZi<／p>
 *
 * @author wjming
 * @version 1.0
 * @date 2018年12月10日
 */
public class ChangJingMianBan0Faa8A02 implements Device {
    private static final String WAY_NUMBER_1 = "1";
    private static final String WAY_NUMBER_2 = "2";
    private static final String WAY_NUMBER_3 = "3";

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";

        if (ChangJingMianBan0Faa8A02CmdDeal.ControlType.SCENE_NUMBER_SET.equals(controlType)) {
            cmd = getSceneNumberSetCmd(map);
        } else if (ChangJingMianBan0Faa8A02CmdDeal.ControlType.SCENE_NUMBER_GET.equals(controlType)) {
            cmd = getSceneNumberGetCmd(map);
        }
        return cmd;
    }

    /**
     * 获取场景编号配置命令
     *
     * @param map
     * @return
     */
    private String getSceneNumberSetCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd8 = "";
        String wayNumber = actionsMap.get("wayNumber");
        if (WAY_NUMBER_1.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_8_ONE;
        } else if (WAY_NUMBER_2.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_8_TWO;
        } else if (WAY_NUMBER_3.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_8_THREE;
        }

        String cmd3To10 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_3 +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_7 +
                cmd8 +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_9 +
                CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("sceneNumber")));

        String cmd2 = CmdUtils.getDateLength(cmd3To10);

        String cmd11 = CmdUtils.getCmdBodySum(cmd3To10);

        String fullCmd = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_1 +
                cmd2 +
                cmd3To10 +
                cmd11 +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberSet.CMD_12;

        return fullCmd;
    }


    /**
     * 获取场景编号获取的命令
     *
     * @param map
     * @return
     */
    private String getSceneNumberGetCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd8 = "";
        String wayNumber = actionsMap.get("wayNumber");
        if (WAY_NUMBER_1.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_8_ONE;
        } else if (WAY_NUMBER_2.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_8_TWO;
        } else if (WAY_NUMBER_3.equals(wayNumber)) {
            cmd8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_8_THREE;
        }

        String cmd3To8 = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_3 +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_7 +
                cmd8;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                ChangJingMianBan0Faa8A02CmdDeal.SceneNumberGet.CMD_10;

        return fullCmd;
    }


}
